import Vue from 'vue'

import VueRouter from 'vue-router'

import App from './App.vue'
import router from './router'

import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)

//导入flexible设置rem基准值
import 'lib-flexible/flexible'

//导入vant组件//注册vant组件
import { Tabbar, TabbarItem, Search } from 'vant';
import { Sidebar, SidebarItem } from 'vant';
import { Skeleton, Toast } from 'vant';
import { NavBar } from 'vant';
import { GoodsAction, GoodsActionIcon, GoodsActionButton } from 'vant';
import { Stepper } from 'vant';
import { Form } from 'vant';
import { Field } from 'vant';
import { Button } from 'vant';
import { Popup } from 'vant';
import { SwipeCell } from 'vant';
import { Checkbox, CheckboxGroup } from 'vant';
import { SubmitBar } from 'vant';
import { Empty } from 'vant';
import { Cell, CellGroup } from 'vant';
import { AddressList } from 'vant';
import { AddressEdit } from 'vant';
import { Uploader } from 'vant';
import { Swipe, SwipeItem } from 'vant';
import { Icon } from 'vant';
import { Switch } from 'vant';
import { Tag } from 'vant';
import { PasswordInput, NumberKeyboard } from 'vant';
import { Overlay } from 'vant';
import { Tab, Tabs } from 'vant';

Vue.use(Tab);
Vue.use(Tabs);
Vue.use(Overlay);
Vue.use(Tabbar)
Vue.use(TabbarItem)
Vue.use(Search)
Vue.use(Sidebar);
Vue.use(SidebarItem);
Vue.use(Skeleton)
Vue.use(Toast)
Vue.use(NavBar);
Vue.use(GoodsAction);
Vue.use(GoodsActionButton);
Vue.use(GoodsActionIcon);
Vue.use(Stepper);
Vue.use(Form);
Vue.use(Field);
Vue.use(Button);
Vue.use(Popup);
Vue.use(SwipeCell);
Vue.use(Checkbox);
Vue.use(CheckboxGroup);
Vue.use(SubmitBar);
Vue.use(Empty);
Vue.use(Cell);
Vue.use(CellGroup);
Vue.use(AddressList);
Vue.use(AddressEdit);
Vue.use(Uploader);
Vue.use(Swipe);
Vue.use(SwipeItem);
Vue.use(Icon);
Vue.use(Switch);
Vue.use(Tag);
Vue.use(PasswordInput);
Vue.use(NumberKeyboard);



Vue.config.productionTip = false

//防止点击相同路由出现错误(此错误不会后续操作)
let naturalPush = VueRouter.prototype.push;

//重写路由原型的push方法, 在push方法添加catch方法捕获错误
VueRouter.prototype.push = function (location) {
    return naturalPush.call(this, location).catch(err => { });
}




import { needToken } from './js/needToken'

//设置默认基础请求路径
axios.defaults.baseURL = 'http://www.kangliuyong.com:10002';

// 添加请求拦截器(请求之前触发)
axios.interceptors.request.use(config => {
    // 在发送请求之前做些什么
    

    // 存储好 appkey 和 token，追加进 请求数据中 
    let appkey = 'U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=';
    let tokenString = localStorage.getItem("token");

    if (config.method === 'get') {
        config.params = config.params || {};
        config.params.appkey = appkey;

        // 追加 token 参数
        if (needToken.indexOf(config.url) > -1) {
            config.params.tokenString = tokenString;
        }
        
    }
    else if (config.method === 'post') {
        config.data = config.data || {};

        // 追加 token 参数
        if (needToken.indexOf(config.url) > -1) {
            config.data.tokenString = tokenString;
        }

        // data参数 整合成 请求格式
        let data = "";
        for (let key in config.data) {
            data += `${key}=${config.data[key]}&`;
        }
        data += `appkey=${appkey}`;
        config.data = data;

        
    }



    //启动加载提示
    Toast.loading({
        message: '加载中...',
        duration: 0
    });

    return config;
}, error => {
    // 对请求错误做些什么
    //关闭加载提示
    Toast.clear();
    return Promise.reject(error);
});


// 添加响应拦截器(响应之前触发)
axios.interceptors.response.use(response => {
    // 对响应数据做点什么
    

    //关闭加载提示
    Toast.clear();

    return response;
}, error => {
    // 对响应错误做点什么
    //关闭加载提示
    Toast.clear();
    return Promise.reject(error);
});

new Vue({
    router,
    render: h => h(App)
}).$mount('#app')
